package com.fangyang.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fangyang.domain.User;
import com.fangyang.service.UserServcie;

/**
 * 用户Controller
 * 
 * @author fangyang
 * @date 2017年9月15日
 * @version 2.0
 */
@Controller
public class UserController {
    @Autowired
    private UserServcie userServcie;

    /**
     * springBoot测试方法
     * 
     * @return
     */
    @RequestMapping("/user/test")
    @ResponseBody
    public String springBootTest() {

        return "hello,spring boot";
    }

    /**
     * 查找所有用户
     * 
     * @return
     */
    @RequestMapping("/user/findAll")
    @ResponseBody
    public List<User> findAll() {
        List<User> userList = userServcie.findAll();
        return userList;
    }

    /**
     * 根据id来查找用户
     * 
     * @param id 用户编号
     * @return
     */
    @RequestMapping("/user/findOne/{id}")
    @ResponseBody
    public User findById(@PathVariable Integer id) {
        User user = userServcie.findById(id);
        return user;
    }

    /**
     * 用户登录
     * 
     * @param username
     * @param password
     * @param ssession
     * @return
     */

    @RequestMapping("/user/login")
    public String login(String username, String password, HttpSession ssession) {
        User user = userServcie.findByUsernameAndPassword(username, password);
        if (user != null) {
            ssession.setAttribute("user", user);
            return "success";
        }
        return "faid";
    }

}
